package f_socketChat;

import java.io.DataInputStream;
import java.io.IOException;
import java.net.Socket;

/**
 * 接收线程
 * @author Y
 *
 */
public class Receive implements Runnable{

	//输入流
	private DataInputStream dis;
	//线程控制标识
	private boolean isRunning = true;
	
	//提供无参构造方法
	public Receive() {
	}

	//提供有参构造方法
	public Receive(Socket client) {
		try {
			dis = new DataInputStream(client.getInputStream());
		} catch (IOException e) {
			isRunning = false;
			CloseUtil.closeAll(dis);
		}
	}
	
	/**
	 * 接收数据
	 * @return
	 */
	public String receive(){
		String msg= "";
		try {
			//从输入流读取数据
			msg = dis.readUTF();
		} catch (IOException e) {
			isRunning=false;
			CloseUtil.closeAll(dis);
		}
		return msg;
	}


	@Override
	public void run() {
		//线程体
		while (isRunning) {
			//打印读取的数据
			System.out.println(receive());
		}
		
	}

}
